package com.ruoyi.combat.service.apprisk.impl;

import com.github.pagehelper.PageInfo;
import com.ruoyi.combat.domain.apprisk.*;
import com.ruoyi.combat.mapper.apprisk.CombatApplicationMapper;
import com.ruoyi.combat.mapper.apprisk.CombatPhoneMapper;
import com.ruoyi.combat.service.apprisk.*;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CombatApplicationScreenServiceImpl implements CombatApplicationScreenService {

    @Autowired
    private ICombatAppRiskService combatAppRiskService;
    @Autowired
//    private ICombatApplicationService combatApplicationService;
    private CombatApplicationMapper combatApplicationMapper;
    @Autowired
    private ICombatPhoneAppService combatPhoneAppService;
    @Autowired
    private CombatPhoneMapper combatPhoneMapper;
    @Autowired
    private ICombatPhoneService combatPhoneService;

    @Autowired
    private ISysUserService sysUserService;

    @Override
    public TableDataInfo warnList() {
        List<CombatAppRisk> combatAppRisks = combatAppRiskService.selectCombatAppRiskList(new CombatAppRisk());
        List<AppRiskUserInfo> list = new ArrayList<>();
        for (CombatAppRisk combatAppRisk : combatAppRisks){
            Long appId = combatAppRisk.getAppId();
            CombatApplication combatApplication = combatApplicationMapper.selectCombatApplicationByAppid(appId);
            if(combatApplication!=null) {
                CombatPhoneApp combatPhoneApp = new CombatPhoneApp();
                combatPhoneApp.setAppId(appId);
                List<CombatPhoneApp> combatPhoneApps = combatPhoneAppService.selectCombatPhoneAppList(combatPhoneApp);
                for (CombatPhoneApp combatPhoneApp1 : combatPhoneApps) {
                    Long phoneId = combatPhoneApp1.getPhoneId();
                    CombatPhone combatPhone = combatPhoneMapper.selectCombatPhoneByPhoneId(phoneId);
                    if (combatPhone != null) {
                        SysUser user = new SysUser();
                        user.setPhonenumber(combatPhone.getPhoneNum());
                        List<SysUser> sysUsers = sysUserService.selectUserList(user);
                        for (SysUser user1 : sysUsers) {
                            try {
                                AppRiskUserInfo info = new AppRiskUserInfo();
                                info.setUserName(user1.getNickName());
                                info.setAppName(combatApplication.getAppName());
                                info.setAppRiskContent(combatAppRisk.getRiskContent());
                                info.setCombatApplication(combatApplication);
                                info.setCombatPhone(combatPhone);
                                list.add(info);
                            } catch (Exception e) {
                                e.printStackTrace();
//                            throw new RuntimeException(e);
                            }
                        }
                    }
                }
            }
        }

//        int pageNum = new PageInfo(combatAppRisks).getPageNum();
//        int pageSize = new PageInfo(combatAppRisks).getPageSize();
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(list.size());
        return rspData;
    }

    @Override
    public List<Map<String, String>> userAppNum() {
        List<Map<String, String>> stringLongMap = combatAppRiskService.userAppNum();
        List<Map<String, String>> list = new ArrayList<>();
        stringLongMap.stream().forEach(map -> {
            String phoneNum = map.get("phone_num");
            if(StringUtils.isNotEmpty(phoneNum)) {
                SysUser user = new SysUser();
                user.setPhonenumber(phoneNum);
                List<SysUser> sysUsers = sysUserService.selectUserList(user);
                if (sysUsers != null && sysUsers.size() > 0) {
                    Map<String, String> data = new HashMap<>();
                    data.put("name", sysUsers.get(0).getNickName());
                    data.put("value", map.get("num"));
                    list.add(data);
                }
            }
        });
       /* stringLongMap.stream().map(map -> {

        });*/



        return list;
    }

    @Override
    public List<Map<String, String>> appRiskCount() {
        return combatAppRiskService.appRiskCount();
    }

    @Override
    public Map<String,String> appCheckCount() {
        Map<String,String> data = new HashMap<>();
        //检测到风险的apk数量
        Long l = combatAppRiskService.appCount();
        //apk总数
        List<CombatApplication> combatApplications = combatApplicationMapper.selectCombatApplicationList(new CombatApplication());
        int size = combatApplications.size();
        //未检测到风险的apk数量
        int i = size - l.intValue();
        //app总数
        data.put("total",String.valueOf(size));
        //无风险app数
        data.put("unrisk",String.valueOf(i));
        //有风险app数
        data.put("risk",String.valueOf(l));
        return data;
    }
}
